package com.wang.lottery.domain.rule.service.logic;

import com.wang.lottery.common.Constants;
import com.wang.lottery.domain.rule.model.po.RuleTreeNodeLine;

import java.util.List;

/**
 * @author wjh
 * @create 2023-11-11 17:30
 * 规则基础抽象类
 */
public abstract class BaseLogic implements LogicFilter{
    
    
    /**
     * 逻辑决策器
     *
     * @param matterValue          决策值
     * @param ruleTreeNodeLineList 决策节点路线
     * @return 下一个节点Id
     */
    @Override
    public Long filter(String matterValue, List<RuleTreeNodeLine> ruleTreeNodeLineList) {
        for (RuleTreeNodeLine ruleTreeNodeLine : ruleTreeNodeLineList) {
            if (decisionLogic(matterValue, ruleTreeNodeLine)) {
                return ruleTreeNodeLine.getNodeIdTo();
            }
        }
        return Constants.Global.TREE_NULL_NODE;
    }

    private boolean decisionLogic(String matterValue, RuleTreeNodeLine ruleTreeNodeLine) {
        switch (ruleTreeNodeLine.getRuleLimitType()) {
            case Constants.RuleLimitType.EQUAL: {
                return matterValue.equals(ruleTreeNodeLine.getRuleLimitValue());
            }
            case Constants.RuleLimitType.GT: {
                return Double.parseDouble(matterValue) > Double.parseDouble(ruleTreeNodeLine.getRuleLimitValue());
            }
            case Constants.RuleLimitType.LT: {
                return Double.parseDouble(matterValue) < Double.parseDouble(ruleTreeNodeLine.getRuleLimitValue());
            }
            case Constants.RuleLimitType.GE: {
                return Double.parseDouble(matterValue) >= Double.parseDouble(ruleTreeNodeLine.getRuleLimitValue());
            }
            case Constants.RuleLimitType.LE: {
                return Double.parseDouble(matterValue) <= Double.parseDouble(ruleTreeNodeLine.getRuleLimitValue());
            }
            default: {
                return false;
            }
        }
    }
}
